package com.sm.healthy.ui.fragment

import android.graphics.Color
import android.os.Bundle
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.lifecycle.MutableLiveData
import com.afollestad.date.dayOfMonth
import com.afollestad.date.month
import com.afollestad.date.year
import com.bigkoo.pickerview.builder.TimePickerBuilder
import com.bigkoo.pickerview.listener.OnTimeSelectListener
import com.bigkoo.pickerview.view.TimePickerView
import com.sm.healthy.R
import com.sm.healthy.base.BaseFragment
import com.sm.healthy.base.handle.TopHandle
import com.sm.healthy.data.bean.UserEntity
import com.sm.healthy.databinding.FragmentUserCenterBinding
import com.sm.healthy.ext.getTimeFormat
import com.sm.healthy.model.UserCenterAddViewModel
import me.hgj.jetpackmvvm.ext.nav
import java.util.*

/**
 * A simple [Fragment] subclass.
 * Use the [UserCenterFragment.newInstance] factory method to
 * create an instance of this fragment.
 */
class UserCenterFragment : BaseFragment<UserCenterAddViewModel, FragmentUserCenterBinding>() {

    var isUpdateUserInfo = true

    val pickerView: TimePickerView by lazy {
        TimePickerBuilder(requireContext(),
            OnTimeSelectListener { date, _ ->
                mViewModel.userInfo.value?.apply {
                    birthday = date.getTimeFormat("yyyy/MM/dd")
                    mViewModel.userInfo.postValue(this)
                }
                mDatabind.tvBirthday.text = date.getTimeFormat("yyyy/MM/dd")
            })
            .setLayoutRes(R.layout.layout_time_picker) {
                it.findViewById<TextView>(R.id.btnCancel).setOnClickListener {
                    pickerView.dismiss()
                }
                it.findViewById<TextView>(R.id.btnSubmit).setOnClickListener {
                    pickerView.returnData()
                    pickerView.dismiss()
                }
            }
            .setRangDate(
                Calendar.getInstance().apply {
                    year = 1910
                    month = 0
                    dayOfMonth = 1
                },
                Calendar.getInstance()
            ).setDate(Calendar.getInstance())
            .setContentTextSize(10)
            .isDialog(false)
            .setOutSideCancelable(false)
            .setBgColor(Color.parseColor("#FFFFFF")).build()
    }

    override fun layoutId(): Int {
        return R.layout.fragment_user_center
    }


    override fun createObserver() {
        super.createObserver()
//        mViewModel.getCustomers().observe(this) {
//            // TODO: 2021/12/14 回调的数据
//        }

        arguments?.getParcelable<UserEntity>("userEntity")?.let {
            mViewModel.userInfo.value = it
            val header = it.headerImg
            when (header) {
                1 -> mDatabind.rb1.isChecked = true
                2 -> mDatabind.rb2.isChecked = true
                3 -> mDatabind.rb3.isChecked = true
                4 -> mDatabind.rb4.isChecked = true
                5 -> mDatabind.rb5.isChecked = true
                6 -> mDatabind.rb6.isChecked = true
                0 -> mDatabind.rb7.isChecked = true
            }
            isUpdateUserInfo = false
        }

        mViewModel.isMan.observe(this) {
            mViewModel.userInfo.value?.sex = if (it) 0 else 1
        }
    }

    override fun initView(savedInstanceState: Bundle?) {
        mDatabind.click = Click()
        mDatabind.model = mViewModel
        mDatabind.handle = TopHandle(nav())

//        mViewModel.userInfo.value = UserEntity(0,"1","name",1,"2021/12/16","17722923232","外公",1)
    }

    inner class Click() {

        fun llBirthday() {
            pickerView.show()
        }

        fun cancel() {
            nav().navigateUp()
        }

        fun confirm() {
            mViewModel.userInfo.value?.apply {
                name = mDatabind.etName.text.toString().trim()
                role = mDatabind.etRole.text.toString().trim()
                mobile = mDatabind.etMobile.text.toString().trim()
            }
            if (isUpdateUserInfo) {
                mViewModel.insert()
            } else {
                mViewModel.update()
            }
            nav().navigateUp()
        }

        fun rbClick1() {
            mDatabind.rb1.isChecked = true
            mDatabind.rb2.isChecked = false
            mDatabind.rb3.isChecked = false
            mDatabind.rb4.isChecked = false
            mDatabind.rb5.isChecked = false
            mDatabind.rb6.isChecked = false
            mDatabind.rb7.isChecked = false
            mViewModel.userInfo.value?.headerImg = 1;
        }

        fun rbClick2() {
            mDatabind.rb1.isChecked = false
            mDatabind.rb2.isChecked = true
            mDatabind.rb3.isChecked = false
            mDatabind.rb4.isChecked = false
            mDatabind.rb5.isChecked = false
            mDatabind.rb6.isChecked = false
            mDatabind.rb7.isChecked = false
            mViewModel.userInfo.value?.headerImg = 2
        }

        fun rbClick3() {
            mDatabind.rb1.isChecked = false
            mDatabind.rb2.isChecked = false
            mDatabind.rb3.isChecked = true
            mDatabind.rb4.isChecked = false
            mDatabind.rb5.isChecked = false
            mDatabind.rb6.isChecked = false
            mDatabind.rb7.isChecked = false
            mViewModel.userInfo.value?.headerImg = 3
        }

        fun rbClick4() {
            mDatabind.rb1.isChecked = false
            mDatabind.rb2.isChecked = false
            mDatabind.rb3.isChecked = false
            mDatabind.rb4.isChecked = true
            mDatabind.rb5.isChecked = false
            mDatabind.rb6.isChecked = false
            mDatabind.rb7.isChecked = false
            mViewModel.userInfo.value?.headerImg = 4
        }

        fun rbClick5() {
            mDatabind.rb1.isChecked = false
            mDatabind.rb2.isChecked = false
            mDatabind.rb3.isChecked = false
            mDatabind.rb4.isChecked = false
            mDatabind.rb5.isChecked = true
            mDatabind.rb6.isChecked = false
            mDatabind.rb7.isChecked = false
            mViewModel.userInfo.value?.headerImg = 5
        }

        fun rbClick6() {
            mDatabind.rb1.isChecked = false
            mDatabind.rb2.isChecked = false
            mDatabind.rb3.isChecked = false
            mDatabind.rb4.isChecked = false
            mDatabind.rb5.isChecked = false
            mDatabind.rb6.isChecked = true
            mDatabind.rb7.isChecked = false
            mViewModel.userInfo.value?.headerImg = 6
        }

        fun rbClick7() {
            mDatabind.rb1.isChecked = false
            mDatabind.rb2.isChecked = false
            mDatabind.rb3.isChecked = false
            mDatabind.rb4.isChecked = false
            mDatabind.rb5.isChecked = false
            mDatabind.rb6.isChecked = false
            mDatabind.rb7.isChecked = true
            mViewModel.userInfo.value?.headerImg = 0
        }
    }
}